package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.dto.MessageDto;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.AdminVideoVO;
import com.tanhua.model.vo.MovementsVO;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Xiao
 * @version 1.0
 * @description
 * @date 2021/11/3
 */
@Service
public class VideosService {
    @DubboReference
    private VideoApi videoApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    /**
     * 视频管理-视频记录翻页
     * @param messageDto    pagesize、page、uid(用户id)
     * @return
     */
    public PageResult getVideoList(MessageDto messageDto) {
        PageResult pageResult = new PageResult();
        if (ObjectUtil.isEmpty(messageDto)) {
            return pageResult;
        }
        Integer page = messageDto.getPage();
        Integer pagesize = messageDto.getPagesize();
        Long uid = messageDto.getUid();
        pageResult = videoApi.getVideoList(page, pagesize, uid);
        List<Video> videos = (List<Video>) pageResult.getItems();
        if (CollUtil.isEmpty(videos)) {
            return pageResult;
        }
        List<Long> userIds = CollUtil.getFieldValues(videos, "userId", Long.class);
        Map<Long, UserInfo> infoMap = userInfoApi.selectByIdsAndCondition(userIds, null);
        List<AdminVideoVO> adminVideoVOs = new ArrayList<>();
        for (Video video : videos) {
            UserInfo userInfo = infoMap.get(video.getUserId());
            if (ObjectUtil.isNotEmpty(userInfo)) {
                adminVideoVOs.add(AdminVideoVO.init(userInfo, video));
            }
        }
        pageResult.setItems(adminVideoVOs);
        return pageResult;
    }
}
